package com.clusterswisdom.horn.api.tmall.action.bill;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.clusterswisdom.horn.api.ioc.HornAction;
import com.clusterswisdom.horn.api.ioc.HornResponse;
import com.clusterswisdom.horn.api.ioc.api.ApiConfig;
import com.clusterswisdom.horn.api.tmall.TmallInstance;
import com.clusterswisdom.horn.api.tmall.action.AbstractTmallAction;
import com.clusterswisdom.horn.api.tmall.action.bill.event.EbizBill;
import com.taobao.api.domain.Bill;
import com.taobao.api.domain.Item;
import com.taobao.api.response.BillBillsGetResponse;
import com.taobao.api.response.ItemGetResponse;

/**
 * 获取财务结算一览
 * 
 * @author bin
 *
 */
public class GetFinancialSettlementAction extends AbstractTmallAction implements HornAction {

	@Override
	public HornResponse execute(ApiConfig config, JSONObject param)
			throws Exception {
		List<EbizBill> ebizBills=getEbizBills(param);
		return new HornResponse(true, ebizBills);
	}
	
	private List<Object> getData(String finacialDate) {
		return null;
	}
	/**
	 * 获取ebiz需要的账单集合
	 * @throws Exception 
	 */
	public List<EbizBill> getEbizBills(JSONObject param) throws Exception{
		//获取账期
		String finacialDate = getFinanialDate(param);
		//根据账期解析，得到开始时间和结束时间
		HashMap<String,Date> time=getDate(finacialDate);
		Date firDate=time.get("firDate");
		Date secDate=time.get("secDate");
		Long start=firDate.getTime();
		Long end=secDate.getTime();
		
		//要传入到api中的JSONObject(未考虑账期是否大于1天)//等会写循环查找
		JSONObject request=getJSONObject(firDate, secDate);
		
		//获取账单的信息
		List<Bill> bills=getBills(request);
		
		//获取ebiz需要的账单集合
		List<EbizBill> ebizBills=getEbizBills(bills);
		return ebizBills;
	}
	
	private String getFinanialDate(JSONObject param) {
		//TODO 修改参数名称
		return param.getString("date");
	}
	
	/**
	 * 获取账期开始时间和结束时间
	 * @param finacialDate
	 * @return
	 * @throws ParseException
	 * 
	 */
	private HashMap<String,Date> getDate(String finacialDate) throws ParseException{
		int charNum=finacialDate.charAt('到');
		String firDate=finacialDate.substring(0, charNum);
		String secDate=finacialDate.substring(charNum+1);
		
		SimpleDateFormat sdf=new SimpleDateFormat("YY-MM-DD hh:mm:ss");
		Date startDate=sdf.parse(firDate);
		Date endDate=sdf.parse(secDate);
		HashMap<String,Date> startAndEnd=new HashMap<String, Date>();
		startAndEnd.put("firDate", startDate);
		startAndEnd.put("secDate", endDate);
		return startAndEnd;
	}
	
	/**
	 * 要传入到根据账期查询账单的天猫api中的数据
	 * @param firDate
	 * @param secDate
	 * @return
	 * 
	 */
	private JSONObject getJSONObject(Date firDate,Date secDate){
		JSONObject param=new JSONObject();
		param.put("start_time", firDate);
		param.put("end_time", secDate);
		return param;
	}
	
	/**
	 * 根据账期获取账单
	 * @param request
	 * @return
	 * @throws Exception
	 */
	private List<Bill> getBills(JSONObject request) throws Exception{
		BillBillsGetResponse response=(BillBillsGetResponse)TmallInstance.getInstance().executeRequest(request, "bill", "billBillsGet");
		return response.getBills();
	}
	
	/**
	 * 获取ezbiz需要到账单集合
	 * @param bills
	 * @return
	 * @throws Exception
	 */
	private List<EbizBill> getEbizBills(List<Bill> bills) throws Exception{
		//int bid, int account_id, int trade_id, int order_id,int total_amount, int amount, Date pay_time, int status,int num_iid, int post_fee, int tmall_point_fee
		List<EbizBill> ebizBills=new ArrayList<EbizBill>();
		for(Bill bill:bills){
			EbizBill ebizBill=new EbizBill();
//			ebizBill.setBid(bill.getBid());
//			ebizBill.setAccount_id(bill.getAccountId());
//			ebizBill.setTrade_id(bill.getTradeId());
//			ebizBill.setOrder_id(bill.getOrderId());
//			ebizBill.setTotal_amount(bill.getTotalAmount());
//			ebizBill.setAmount(bill.getAmount());
//			ebizBill.setPay_time(bill.getPayTime());
//			ebizBill.setStatus(bill.getStatus());
//			ebizBill.setNum_iid(bill.getNumIid());
			
			//商品时需要传入的JSONObject
			JSONObject param=new JSONObject();
			param.put("num_iid", bill.getNumIid());
			ItemGetResponse response=(ItemGetResponse)TmallInstance.getInstance().executeRequest(param, "item", "itemGet");
			Item item=response.getItem();
			
//			ebizBill.setPost_fee(item.getPostFee());
//			ebizBill.setTmall_polong_fee(item.getAuctionPoint());
			
			ebizBills.add(ebizBill);
		}
		
		return ebizBills;
	}
}